package com.hongshan.wallet.core.home

import android.app.Application
import android.databinding.ObservableField
import android.view.View
import com.hongshan.wallet.Constant
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseOnPropertyChangedCallback
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.base.SingleLiveEvent
import com.hongshan.wallet.bean.PaymentMethod

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : zhangxx
 * @date     : 2019/4/10
 * @email    : qq957413212@hotmail.com
 * @desc     : //TODO
 */
class PayMethodRecycleViewModel(application: Application) : MyViewModel(application) {

    var position = 0

    val checked = ObservableField(false)

    val bankName = ObservableField("")

    val bankIcon = ObservableField("")

    val aliOrWxIcon = ObservableField<Int>()

    val otherUncheck = SingleLiveEvent<Int>()

    val next = SingleLiveEvent<Unit>()

    val payMethod = ObservableField<PaymentMethod>()

    val click = View.OnClickListener {
        val check = checked.get() ?: false
        if (!check) {
            checked.set(true)
            otherUncheck.value = position
        }
    }

    val nextTopUp = View.OnClickListener {
        next.call()
    }

    init {
        payMethod.addOnPropertyChangedCallback(BaseOnPropertyChangedCallback<ObservableField<PaymentMethod>> { sender, _ ->
            val bean = sender.get()
            if (bean != null) {
                bankName.set(bean.name)
                bankIcon.set(Constant.HttpApi.BASE_URL+bean.log_url)
                when (bean.code) {
                    PaymentMethod.CODE_ALIPAY -> {
                        aliOrWxIcon.set(R.drawable.ic_zfb)
                    }
                    PaymentMethod.CODE_WECHAT -> {
                        aliOrWxIcon.set(R.drawable.ic_wechat_green)
                    }
                }
            }
        })
    }
}